package com.huike.controller;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.huike.entity.HistoryEvent;
import com.huike.service.IHistoryEventService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Lwen
 * @since 2019-11-12
 */
@RestController
@RequestMapping("/history-event")
public class HistoryEventController {

	@Autowired
	IHistoryEventService iHistoryEventService;

	/**
	 * 获取后台数据中的所有历史大事件信息,并展示在首页
	 * 
	 * @return
	 */
	@RequestMapping("/getHistoryEventAction")
	@ResponseBody
	public List<HistoryEvent> getHistoryEventAction() {
		List<HistoryEvent> historyEvents = iHistoryEventService.list();
		Collections.sort(historyEvents, new Comparator<HistoryEvent>() {
			@Override
			public int compare(HistoryEvent o1, HistoryEvent o2) {
				// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd
				// HH:mm:ss");
				try {
					Date dt1 = o1.getTime();
					Date dt2 = o2.getTime();
					if (dt1.getTime() > dt2.getTime()) {
						return -1;
					} else if (dt1.getTime() < dt2.getTime()) {
						return 1;
					} else {
						return 0;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 0;
			}
		});
		return historyEvents;
	}

}
